package billings.classtasks.classes;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import billings.classtasks.BaseDAO;
import billings.classtasks.DatabaseHelper;
import billings.classtasks.tasks.Task;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class ClassDAO extends BaseDAO{

	//db fields
	private String[] allColumns = { DatabaseHelper.CLASS_COLUMN_ID,
			                        DatabaseHelper.CLASS_COURSE_NUMBER,
			                        DatabaseHelper.CLASS_COURSE_MAJOR,
			                        DatabaseHelper.CLASS_COURSE_PROFESSOR };
	
	public ClassDAO(Context context) {
		super(context);
	}
	
	public Class createClass(String courseNumber, String courseMajor, String courseProfessor){
		
		//setup content values
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.CLASS_COURSE_NUMBER, courseNumber);
		values.put(DatabaseHelper.CLASS_COURSE_MAJOR, courseMajor);
		values.put(DatabaseHelper.CLASS_COURSE_PROFESSOR, courseProfessor);
		
		long insertId = database.insert(DatabaseHelper.CLASS_TABLE, null, values);
		Cursor cursor = database.query(DatabaseHelper.CLASS_TABLE, allColumns, 
				                       DatabaseHelper.CLASS_COLUMN_ID + " = " + insertId, 
				                       null, null, null, null);
		
		cursor.moveToFirst();		
		Class newClass = cursorToClass(cursor);
		cursor.close();
		
		return newClass;
	}
	
	public Class createClass(String courseNumber, String courseMajor){
		//setup content values
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.CLASS_COURSE_NUMBER, courseNumber);
		values.put(DatabaseHelper.CLASS_COURSE_MAJOR, courseMajor);		
		
		long insertId = database.insert(DatabaseHelper.CLASS_TABLE, null, values);
		Cursor cursor = database.query(DatabaseHelper.CLASS_TABLE, allColumns, 
							           DatabaseHelper.CLASS_COLUMN_ID + " = " + insertId, 
						               null, null, null, null);
				
		cursor.moveToFirst();		
		Class newClass = cursorToClass(cursor);
		cursor.close(); 
		
		return newClass;
	}
	
	public int updateClass(long classId, String classCourseNumber, String classCourseMajor, String classCourseProfessor){
		long id = classId;
		ContentValues cvs = new ContentValues();
		cvs.put(DatabaseHelper.CLASS_COURSE_NUMBER, classCourseNumber);
		cvs.put(DatabaseHelper.CLASS_COURSE_MAJOR, classCourseMajor);
		cvs.put(DatabaseHelper.CLASS_COURSE_PROFESSOR, classCourseProfessor);
		
		int rowsUpdated = database.update(DatabaseHelper.CLASS_TABLE, cvs, DatabaseHelper.CLASS_COLUMN_ID + " = ", new String[]{id+""});
		
		return rowsUpdated;
	}
	
	public void deleteClass(int classId){
		int ret = database.delete(DatabaseHelper.CLASS_TABLE, DatabaseHelper.CLASS_COLUMN_ID + " = " + classId, null);
		if(ret > 0){
			System.out.println("Class deleted with id: " + classId);
		}else{
			System.out.println("NO Class was deleted with id: " + classId);
		}
	}
	
	public List<Class> getAllClasses(){
		List<Class> classes = new ArrayList<Class>();
		
		Cursor cursor = database.query(DatabaseHelper.CLASS_TABLE, allColumns, null, null, null, null, null);
		
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			Class cls = cursorToClass(cursor);
			classes.add(cls);
			cursor.moveToNext();
		}
		
		cursor.close();
		
		return classes;
	}
	
	private Class cursorToClass(Cursor cursor){
		Class cls = new Class();
		cls.setId(cursor.getInt(0));
		cls.setCourseNumber(cursor.getString(1));
		cls.setCourseMajor(cursor.getString(2));
		cls.setCourseProfessor(cursor.getString(3));
		return cls;
	}
}
